﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RpcGenerator
{
    class NamespaceGenerator
    {
        private static List<string> CSharpDefault = new List<string>()
                                {                                       
                                    "System", 
                                    "System.Collections.Generic",
                                    "MessageSystem.Managed.Rpc", 
                                    "MessageSystem.Managed.Serialization" ,
                                    "MessageSystem.Managed.Infrastructure",
                                    "MessageSystem.Managed.Interface",
                                    "System.Runtime.Serialization"
                                };

        public MessageNamespace Generate(Type type, List<MessageDescription> descriptions, NamespaceType namespaceType)
        {
            MessageNamespace result = new MessageNamespace();
            result.Namespace = type.Namespace;
            result.ReferenceNamespace = GenerateDefault(namespaceType);
            descriptions.ForEach(description =>
            {
                description.Fields.ForEach(fieldDescription =>
                {
                    fieldDescription.Type.GetProperties().ToList().ForEach(pi =>
                    {
                        string name = pi.PropertyType.Namespace;
                        if (!result.ReferenceNamespace.Contains(name))
                        {
                            if (namespaceType != NamespaceType.CSharp&& !CSharpDefault.Contains(name))
                            {
                                result.ReferenceNamespace.Add(name);
                            }                            
                        }
                    });
                });
            });

            return result;
        }

        public MessageNamespace Generate(MessageFieldDescription description,NamespaceType namespaceType)
        {
            MessageNamespace result = new MessageNamespace();
            result.Namespace = description.Type.Namespace;
            result.ReferenceNamespace = GenerateDefault(namespaceType);
            description.Type.GetProperties().ToList().ForEach(pi =>
            {
                string name = pi.PropertyType.Namespace;
                if (!result.ReferenceNamespace.Contains(name))
                {
                    if (namespaceType != NamespaceType.CSharp && !CSharpDefault.Contains(name))
                    {
                        result.ReferenceNamespace.Add(name);
                    }                    
                }
            });
            return result;
        }

        private List<string> GenerateDefault(NamespaceType namespaceType)
        {
            switch (namespaceType)
            {
                case NamespaceType.CSharp:
                    {
                        return CSharpDefault;
                    }
                case NamespaceType.AS3:
                    {
                        return new List<string>() { "MessageSystemRpc.*", "MessageSystem.*" };
                    }
                case NamespaceType.Cpp:
                    {
                        return new List<string>();
                    }
            }

            return null;
        }
    }
}
